package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Orders;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.service.OrderService;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.servlet.http.HttpSession;

@RestController

public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 提交订单
     * @param order
     * @return
     */
    @PostMapping("/order/submit")
    public R submitOrder(@RequestBody Orders order){

        int row = orderService.add(order);

        if(row > 0 ){

            shoppingCartService.clean();

            return R.success("提交订单成功！");
        }
        return R.success("提交订单失败！");

    }


    /**订单分页查看
     * 分页
     * 何先晓
     * @param pageParam
     * @return
     */
    @GetMapping("/order/page")
    public R findPage(PageParam pageParam){
        try {
            IPage iPageList = orderService.findByPage(pageParam);

            return R.success(iPageList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("订单分页查看失败");
        }
    /**
     * 前台用户查询历史订单
     * @return
     */
    @GetMapping("/order/userPage")
    public R userPage(int page , int pageSize ){

        return   R.success(orderService.userPage(page,pageSize));
    }
}
